<?php
/*
 * 区域控制器
 */
class Area extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->library ( 'pagination' );
	}

	public function index($offset = '') {
		$user_name=$this->session->userdata('username');
		if($user_name){
			$this->load->model ( 'area_model', 'contacter' );
			$limit = 10;
			$total = $this->contacter->count_nums ();
			$data['total']=$total;
			$data['offset']=$offset;
			$data ['area'] = $this->contacter->list_areas ( $limit, $offset );
			
			$current_total=count($data ['area']);
			$data['current_total']=$offset+$current_total;
			
			$config ['base_url'] = base_url () . 'index.php/area/index/';
			$config ['total_rows'] = $total;
			$config ['per_page'] = $limit;
			$config['uri_segment'] = 3;
			$config ['num_links'] = 4;
			$config ['first_link'] = '首页';
			$config ['last_link'] = '尾页';
			$this->pagination->initialize ( $config );
			$data ['pag_links'] = $this->pagination->create_links ();
			
			$data ['page_title'] = '区域 — 昊美科技CRM系统';
			$data ['class7'] = 'cur';
			$this->load->view ( 'area_list', $data );
		}else{
			$this->load->view ( 'login');
		}
	}

	function check($name = ''){ /*查询某条记录*/
		$this->db->like('district', urldecode($name));
		$query = $this->db->get('region');
		$data['area'] = $query->result();
		$data ['pag_links'] = '';
		$data ['page_title'] = '区域 — 昊美科技CRM系统';
		$data ['class7'] = 'cur';
		$this->load->view ( 'area_list', $data );
	}

	/*
	 * 查看某条记录
	 */
	public function find($id = '') {
		$query = $this->db->get_where ( 'region', array ('id' => $id ) );
		$find_data['find_area'] = $query->result();
		$this->load->view ( 'area_view', $find_data );
	}

	/*
	 * 查看某条编辑记录
	*/
	public function update($id = '') {
		$query = $this->db->get_where ( 'region', array ('id' => $id ) );
		$update_data['update_area'] = $query->row();
		$this->load->view ( 'area_edit', $update_data );
	}

		/*
	 * 编辑某条纪律
	 */
	public function update_edit() {
		$data_array = $_POST;
		$id = $data_array['id'];
		date_default_timezone_set('PRC');
		$now_time=date('Y-m-d H:i:s', strtotime('now'));
		$data_array['update_time']=$now_time;
		unset($data_array['id']);
		$this->db->where('id', $id);
		$this->db->update('region', $data_array);
		$row=$this->db->affected_rows();
		if($row > 0){
			redirect ( 'area/index/' );
		}else{

		}
	}

	 /* 删除记录
	 */
	public function delete($id = '') {
		$this->db->delete ( 'region', array ('id' => $id ) );
		redirect ( 'area/index/' );
	}

	 /* 新增客户
	 */
	public function show_customer_add(){ /*显示增加页面*/
		$this->load->view ( 'area_add');
	}

	public function insert(){
		$data_array = $_POST;
		date_default_timezone_set('PRC');
		$time=date('Y-m-d H:i:s', strtotime('now'));
		$data_array['create_time']=$time;
		$this->db->insert('region', $data_array);
		$row=$this->db->affected_rows();
		if($row > 0){
			redirect ( 'area/index/' );
		}else{
		}
	}
}
?>
